package jgl.vajra.management.part.login.activity;

import android.os.Handler;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;

import com.orhanobut.logger.Logger;

import java.util.HashMap;
import java.util.Map;

import jgl.vajra.management.R;
import jgl.vajra.management.bean.LoginInfoBean;
import jgl.vajra.management.constant.Constant;
import jgl.vajra.management.databinding.ActivityLoginBinding;
import jgl.vajra.management.manager.AccountManager;
import jgl.vajra.management.manager.IntentManager;
import jgl.vajra.management.part.login.contract.LoginContract;
import jgl.vajra.management.part.login.viewModel.LoginViewModel;
import jgl.vajra.management.utils.CountUtils;
import jgl.vajra.management.utils.RandomUtils;
import jgl.vajra.management.widget.manager.SPManager;
import jgl.vajra.management.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.management.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.management.widget.retrofithelper.encrypt.RSAEncryptManager;
import jgl.vajra.management.widget.retrofithelper.utils.AEACCoder;
import jgl.vajra.management.widget.utils.PhoneUtils;
import jgl.vajra.management.widget.utils.ToastUtils;
import jgl.vajra.management.widget.utils.UIUtils;

@CreateViewModel(LoginViewModel.class)
public class LoginActivity extends BaseMVVMActivity<LoginViewModel, ActivityLoginBinding> implements LoginContract.View {

    private boolean isAgree = false;
    private String byInvitation;

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initData() {
        super.initData();
        mBinding.setView(this);


        requestNetData();
    }

    public void requestNetData() {
        //获取AES密钥
        mViewModel.getAESSecretKey();
    }

    //用户协议
    public void onAgreement1() {
        showAgreementDialogFragment(1);
    }

    //隐私政策
    public void onAgreement2() {
        showAgreementDialogFragment(2);
    }

    public void showAgreementDialogFragment(int type) {
        ToastUtils.showShort("点击了" + type);
    }

    //取消
    @Override
    public void onDismiss() {
        IntentManager.toHomeActivity(LoginActivity.this);
    }


    //获取验证码
    @Override
    public void onVerifyCode() {
        String phone = mBinding.etPhone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showShort("请输入正确的电话号码");
            return;
        }
        getVerifyCode();
    }

    public void getVerifyCode() {
        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AEACCoder.encrypt(mBinding.etPhone.getText().toString().trim() + "&&" + RandomUtils.getRandom()));
        map.put("type", "register");
        mViewModel.getVerifyCode(map);
    }

    @Override
    public void onLogin() {

        String position = mBinding.etPosition.getText().toString().trim();
        if (TextUtils.isEmpty(position)) {
            ToastUtils.showShort("请输入你的职务");
            return;
        }

        String name = mBinding.etName.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            ToastUtils.showShort("请输入你的真实姓名");
            return;
        }

        String phone = mBinding.etPhone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showShort("请输入正确的电话号码");
            return;
        }

        String verifyCode = mBinding.etVerifyCode.getText().toString().trim();
        if (TextUtils.isEmpty(verifyCode)) {
            ToastUtils.showShort("请输入验证码");
            return;
        }


        String encryptPhone = AEACCoder.encrypt(phone);
        Map<String, Object> map = new HashMap<>();
        map.put("phone", encryptPhone);
        map.put("verificationCode", verifyCode);
        map.put("userType", Constant.USER_TYPE);
        map.put("position", position);
        map.put("name", name);
        mViewModel.onLogin(map);

//        IntentManager.toHomeActivity(LoginActivity.this);
    }

    //加盟要求
    @Override
    public void onJoiningRequirements() {
        IntentManager.toJoiningRequirementsActivity(this);
    }

    //AES秘钥
    @Override
    public void returnAESSecretKeySuccess(String secretKey) {
        //RAS公有密钥:MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxxeLN99tL26yZ7NSmZ73mo76kHFi/OXQy4E94zzVnD8o30ROjl9yT1N4QHterUFnxpbB6PTZU0fA68F+lHASVzkuQEEE+BchyjOKcQaBKT/7X7TNWjpYBUYHZ15Q8Sn4kCAvcGqhLkA33IkFdWNEoGwFHuey3BY/P3vNx2PrVT0mjYHh8hMtZzXZgbPcOam1Np5DEwtQZMBoQP6Vr+o0RQVNPFBchvwLJHChHXjq3QY+LPp0iUyNtn/AsZPekI0vMQlOQv1vZCjxKxU2ylfLHB5V0Y0JT+hEEhZ4/a5KNo0K1Ujb4gz026+qfAjOVajqTULe9zoXbQdXd2Mvq1SSVwIDAQAB
        //RAS私有密钥
        String rASPrivateKey = RSAEncryptManager.decryptByPublicString(secretKey);
        SPManager.HomeData.saveRASPrivateKey(rASPrivateKey);
    }

    @Override
    public void returnLoginPasswordSuccess(LoginInfoBean data) {

        SPManager.LoginData.setIsLogin(true);

        IntentManager.toHomeActivity(LoginActivity.this);
        AccountManager.getInstance().saveLoginData(UIUtils.getContext(), data);
        finish();
    }

    @Override
    public void returnVerifyCodeData(int code) {
        if (code==200){
            CountUtils.getCount(mBinding.tvVerifyCode);
        }else {
            mBinding.tvVerifyCode.setClickable(true);
        }
    }
}
